package cn.piesat.scanning.business.xz.service.ecqimpl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.piesat.scanning.business.xz.service.BuildAlgorithmTaskService;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskPathParam;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskRuleParam;
import cn.piesat.scanning.dto.HtScanningMetadataDTO;
import cn.piesat.scanning.service.HtScanningMetadataService;
import cn.piesat.scanning.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Ming
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 13:48
 */
@Service
public class BuildWSCIServiceImpl implements BuildAlgorithmTaskService {

    @Autowired
    private HtScanningMetadataService htScanningMetadataService;
    @Override
    public Map<String, Object> initAlgorithmParams(String adsType, String primaryFile, String filePath, AlgorithmTaskRuleParam taskRuleParam, AlgorithmTaskPathParam taskPathParam, Map<String, Integer> indexParams, String fileDate, String scanningPath,HtScanningMetadataDTO htScanningMetadataDTO) {
        Map<String, Object> algorithmParams = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
        String[] s1 = fileName.split("_");
        String year = s1[9].substring(0, 4);
        String moduleIid = s1[0];
        String productMode = s1[8];
        String level = s1[3];
        String satellite = s1[4];
        String product = s1[1];
        String region = s1[2];
        String startTime = year + "0501000000";
        String endTime = year + "0930000000";
        //查询NDVI5-9月的月合成数据
        List<HtScanningMetadataDTO> ndviList = htScanningMetadataService.findOneByDate(startTime, endTime, level, satellite,
                product, region, productMode, moduleIid, "TIFF");
        List<String> ndviFileList = ndviList.stream().map(x -> taskPathParam.getParameter02() + File.separator + x.getFilePath()).collect(Collectors.toList());
        String ndviJoinList = StrUtil.join(",", ndviFileList);
        //查询NPP5-9月的月合成数据
        List<HtScanningMetadataDTO> nppList = htScanningMetadataService.findOneByDate(startTime, endTime, level, satellite,
                "NPP", region, "POAM", "FOR", "TIFF");
        List<String> nppFileList = nppList.stream().map(x -> taskPathParam.getParameter02() + File.separator + x.getFilePath()).collect(Collectors.toList());
        String nppJoinList = StrUtil.join(",", nppFileList);

        Date date = DateUtil.parse(endTime, "yyyyMMddHHmmss");
        Date start = DateUtil.offsetMonth(date, -60);
        String dateTimeStr = DateUtil.format(start, "yyyyMMddHHmmss");
        //查询NPP五年的的年合成数据
        List<HtScanningMetadataDTO> metadataDTOList = htScanningMetadataService.findOneByDate(dateTimeStr, endTime, level, satellite,
                "NPP", region, "POAY", "FOR", "TIFF");

        List<String> collect = metadataDTOList.stream().map(x -> taskPathParam.getParameter02() + File.separator + x.getFilePath()).collect(Collectors.toList());
        String join = StrUtil.join(",", collect);

        //TODO 测试调整最少一个文件,正式环境改为5
//        if (ndviList.size() < 5 || nppList.size() < 5 || metadataDTOList.size() < 2) {
        if (ndviList.size() < 1 || nppList.size() < 1 || metadataDTOList.size() < 1) {
            algorithmParams.put("parameter01", null);
        } else {
            params.put("ndviM59MFile", ndviJoinList);
            params.put("nppP59MFile", nppJoinList);
            params.put("nppPO5YFile", join);
            params.put("satelliteName", satellite);
            params.put("auxPath", taskPathParam.getParameter03());
            algorithmParams.put("parameter00", adsType);
            algorithmParams.put("parameter01", params);
        }
        return algorithmParams;
    }
}
